<template>
  <div class="container">
    <!--
    <div class="setting">
      <van-icon name="setting" />
    </div>
    -->
    <!-- 学生信息 -->
    <div class="student-profile">
      <div class="avatar" v-if="avatarPath">
        <!-- <img :src="avatarPath" alt="头像" /> -->
        <img :src="avatarPath" alt="" />
      </div>
      <div v-else class="default-img">{{ studentName }}</div>
      <div class="info">
        <h3 class="name">{{ studentName }}</h3>
        <p class="details">学号: {{ studentNo }}</p>
      </div>
    </div>

    <!-- 个人中心 -->
    <div class="personal-center">
      <h3><strong>个人中心</strong></h3>
      <div class="center-grid">
        <div class="center-item" style="background-color: #eff6ff" @click="toHomework">
          <img src="@/assets/img/homework.svg" alt="课后作业" />
          <p>课后作业</p>
        </div>
        <div class="center-item" style="background-color: #f0fdf4">
          <img src="@/assets/img/studychat.svg" alt="学习论坛" />
          <p>学习论坛</p>
        </div>
        <div class="center-item" style="background-color: #faf5ff">
          <img src="@/assets/img/resource.svg" alt="资源库" />
          <p>资源库</p>
        </div>
        <div class="center-item" style="background-color: #fdf2f8">
          <img src="@/assets/img/aiarea.svg" alt="AI专区" />
          <p>AI专区</p>
        </div>
      </div>
    </div>

    <!-- 课程学情分析 -->
    <div class="course-analysis">
      <div class="analysis-header">
        <!-- <van-icon name="bar-chart-o" size="20" /> -->
        <h3><strong>课程学情分析</strong></h3>
      </div>
      <div class="analysis-list">
        <div class="analysis-item">
          <img src="@/assets/img/course1.png" alt="课程" class="course-img" />
          <div class="content">
            <h4>篮球基础课程</h4>
            <div class="details">
              <van-icon name="clock" /><span>12课时</span> <van-icon name="user" /><span
                >李老师</span
              >
            </div>
          </div>
          <van-icon name="arrow" class="arrow-icon" />
        </div>
        <div class="analysis-item">
          <img src="@/assets/img/course1.png" alt="课程" class="course-img" />
          <div class="content">
            <h4>田径基础课程</h4>
            <div class="details">
              <van-icon name="clock" /><span>8课时</span> <van-icon name="user" /><span
                >王老师</span
              >
            </div>
          </div>
          <van-icon name="arrow" class="arrow-icon" />
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, watch } from 'vue'
import { useRouter } from 'vue-router'
import useUserStore from '@/stores/user'
import defaultAvatar from '@/assets/img/icon.png'

const router = useRouter()
const userStore = useUserStore()
const userInfo = computed(() => userStore.userInfo)
const avatarPath = ref('')
const studentName = ref('')
const studentNo = ref('')
// 初始化或更新用户信息
const updateUserInfo = () => {
  const info = userInfo.value
  if (info) {
    avatarPath.value = info.avatarPath || ''
    studentName.value = info.studentName || ''
    studentNo.value = info.studentNo || ''
    console.log('用户信息已更新:', info)
  } else {
    console.log('用户信息为空')
    // 可以选择重置数据或保持现状
  }
}

// 监听 userInfo 的引用变化
watch(
  userInfo,
  (newVal, oldVal) => {
    console.log('userInfo 引用变化:', newVal, oldVal)
    updateUserInfo()
  },
  { immediate: true },
)

// 额外监听 userInfo 对象内部变化（如果需要）
watch(
  () => JSON.stringify(userInfo.value),
  (newJson) => {
    console.log('userInfo 内容变化:', newJson)
    updateUserInfo()
  },
  { immediate: true },
)

onActivated(() => {
  // getHomework()
  console.log('ssss', userInfo)
})
const toHomework = () => {
  router.push('/homework')
}
</script>

<style scoped>
.container {
  padding: 15px;
  background: #f7f8fa;
  min-height: 100vh;
}

.student-profile {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  margin: 30px 0;
  text-align: center;
}

.avatar {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  overflow: hidden;
  margin-bottom: 15px;
}
.default-img {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid #d9d9d9;
  margin-bottom: 15px;
}
.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.info {
  text-align: center;
}

.name {
  font-size: 18px;
  margin: 10px 0;
  font-weight: bold;
}

.details {
  font-size: 14px;
  color: #666;
  .van-icon-clock {
    margin-right: 3px;
    color: #3b82f6;
  }
  .van-icon-user {
    color: #22c55e;
    margin-left: 8px;
    margin-right: 3px;
  }
}

.personal-center {
  margin: 30px 0;
}

.personal-center h3 {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 20px;
}

.center-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 15px;
  padding: 20px;
  background: #fff;
  border-radius: 12px;
}

.center-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 10px 5px;
  border-radius: 8px;
}

.center-item img {
  width: 20px;
  height: 20px;
  margin-bottom: 10px;
}

.center-item p {
  font-size: 12px;
  font-weight: bold;
}

.course-analysis {
  margin: 30px 0;
}

.course-analysis h3 {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 20px;
}

.analysis-list {
  background: #fff;
  border-radius: 12px;
  overflow: hidden;
}

.analysis-item {
  display: flex;
  align-items: center;
  padding: 15px;
  border-bottom: 1px solid #f5f5f5;

  .arrow-icon {
    color: #3b82f6;
    font-size: 14px;
  }
}

.course-img {
  width: 50px;
  height: 50px;
  border-radius: 8px;
  margin-right: 15px;
  object-fit: cover;
}

.content {
  flex: 1;
}

.content h4 {
  font-size: 15px;
  font-weight: bold;
  margin-bottom: 5px;
}

.content p {
  font-size: 12px;
  color: #666;
}
</style>
